package com.woniu.controller;

import com.alibaba.fastjson.JSONArray;
import com.woniu.dto.ActivityDetail;
import com.woniu.dto.ActivityPost;
import com.woniu.dto.MyActivityPost;
import com.woniu.result.CommonResult;
import com.woniu.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 投诉活动
     * @param complaint 投诉信息
     * @return
     */
    @RequestMapping("/complaintSave")
    @ResponseBody
    public CommonResult<String> ComplaintSave(@RequestBody String complaint){
        try {
            //转化成真实的值
            String act = URLDecoder.decode(complaint,"UTF-8");
            //没有上传图片时参数多了一个 = ，不知道为啥
            act = act.replace("=","");
            //将数据转为map
            Map<String,Object> map = JSONArray.parseObject(act);
            //保存动态到数据库
            activityService.save(map);
            return CommonResult.success("发布成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return CommonResult.failed("操作失败");
        }
    }

    /**
     * 发布活动
     * @param activity 活动信息
     * @return
     */
    @RequestMapping("/activitySave")
    @ResponseBody
    public CommonResult<String> ActivitySave(@RequestBody String activity){
        System.out.println("*******--------");
        try {
            //转化成真实的值
            String act = URLDecoder.decode(activity,"UTF-8");
            //没有上传图片时参数多了一个 = ，不知道为啥
            act = act.replace("=","");
            //将数据转为map
            Map<String,Object> map = JSONArray.parseObject(act);
            //保存动态到数据库
            activityService.ActivitySave(map);
            return CommonResult.success("发布成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return CommonResult.failed("操作失败");
        }
    }

    /**
     * 根据id查找活动详情
     * 根据用户名判断是否已经报名
     * @param activityId
     * @param userId
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public CommonResult detail(Integer activityId,Integer userId){
        System.out.println(activityId+":"+userId);
        ActivityDetail activityDetail = activityService.findActivity(activityId,userId);
        return CommonResult.success(activityDetail);
    }

    /**
     * 获取指定活动id的所有活动人
     * @param activityId
     * @return
     */
    @GetMapping("/myactivity")
    @ResponseBody
    public CommonResult myactivity(Integer activityId){
        System.out.println(activityId);
        List<MyActivityPost> activityUsers = activityService.findUserById(activityId);
        return CommonResult.success(activityUsers);
    }

    /**
     * 保存指定用户到活动报名表
     * @param activityId
     * @param userId
     * @return
     */
    @PostMapping("/signUp")
    @ResponseBody
    public CommonResult signUp(Integer activityId,Integer userId){
        System.out.println(activityId+":"+userId);
        String coNum = activityService.saveSignUp(activityId,userId);
        return CommonResult.success(coNum);
    }

    /**
     * 更改申请活动的用户状态为已参加活动
     * @param activityId
     * @param userId
     * @return
     */
    @PutMapping("/updateSignUp")
    @ResponseBody
    public CommonResult updateSignUp(Integer activityId,Integer userId){
        System.out.println(activityId+":"+userId);
        int count = activityService.updateSignUp(activityId,userId);
        if (count>0){
            return CommonResult.success("已添加该用户");
        }else{
            return CommonResult.failed("添加失败");
        }
    }

    /**
     * 删除指定用户的保命信息
     * @param activityId
     * @param userId
     * @return
     */
    @DeleteMapping("/noSignUp")
    @ResponseBody
    public CommonResult noSignUp(Integer activityId,Integer userId){
        System.out.println(activityId+":"+userId);
        activityService.deleteById(activityId,userId);
        return CommonResult.success("成功");
    }

    /**
     * 根据类型获取指定数量的活动数据
     * @param type
     * @param page
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public CommonResult list(Integer type,Integer page){
        System.out.println(type+":"+page);
        //实例化实体类，适合前端需要的数据
        List<ActivityPost> activityPosts = activityService.findByType(type,page);
        System.out.println(activityPosts.size());
        return CommonResult.success(activityPosts);

    }

    /**
     * 活动发起者退款或者移除用户，彻底删掉该用户
     * @param activityId
     * @param userId
     * @return
     */
    @DeleteMapping("/removeUser")
    @ResponseBody
    public CommonResult removeUser(Integer activityId,Integer userId){
        System.out.println(activityId+":"+userId);
        int count = activityService.deleteByActivityIdAndUserId(activityId,userId);
        if (count>0){
            return CommonResult.success("已移除该用户");
        }else{
            return CommonResult.failed("移除失败");
        }
    }

    /**
     * 根据用户id和活动id查找订单编号
     * @param activityId
     * @param userId
     * @return
     */
    @GetMapping("/getCoNum")
    @ResponseBody
    public CommonResult getCoNum(Integer activityId,Integer userId){
        System.out.println(activityId+":"+userId);
        String coNum = activityService.findCoNum(activityId,userId);
        return CommonResult.success(coNum);
    }

    @GetMapping("/getContent")
    @ResponseBody
    public CommonResult getContent(Integer activityId){
        System.out.println(activityId);
        String content = activityService.selectContent(activityId);
        return CommonResult.success(content);
    }

}
